
package services.implementation;

import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import entity.Users;
import services.IRegistrationService;
	
/*
 * @author smile
 */

@Stateless
@LocalBean
public class RegistrationService extends GenericServices implements IRegistrationService {

    @PersistenceContext(unitName = "snPU")
    private EntityManager em;
                    
    @Override
    public void registration(String email, String login, String password, String role) {
        Users user = new Users();
        user.setId(1);
        user.setEmail(email);
        user.setLogin(login);
        user.setPassword(password);
        user.setRoleSux(role);
        this.insert(user);
    }

    @Override
    public boolean checkEmail(String email) {  
        
        if (this.em.createNamedQuery("Users.findByEmail")
                    .setParameter("email", email)
                    .getResultList().isEmpty()) {
            return false;
        }else {
            return true;
        }
    }
    
    public String checkPassword(String password, String repassword) {  
        if (!password.equals(repassword)){
            return "message.password.isnotequal";
        }else{
            if (password.isEmpty()){
                 return "message.password.empty";
            }else{
                if (password.length() < 6){
                    return "message.password.lenghtless";
                } else {
                    return "Successfull";
                }
            }
        }
    }


    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
}